/**
 * 
 */
package com.ims.stocks.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.ims.common.action.BaseAction;
import com.ims.common.utils.CodeValueData;
import com.ims.common.utils.propertyManager;
import com.ims.stocks.bo.EmployeeBO;
import com.ims.stocks.bo.UserBO;
import com.ims.stocks.model.Address;
import com.ims.stocks.model.Employee;
import com.ims.stocks.model.Role;
import com.ims.stocks.model.UserDetails;
import com.ims.stocks.service.address.AddressService;
import com.ims.stocks.service.employee.EmployeeService;
import com.ims.stocks.service.person.PersonService;

/**
 * @author Achal B.I
 *
 */
public class EmployeeAction extends BaseAction implements ServletRequestAware{
	/**
	 * 
	 */
	private static final long serialVersionUID = -7643701038758833988L;
	
	private Employee employee = new Employee();
	private Address address =  new Address();
	private String managerIdStr;
	private String crudType; 
	private EmployeeService employeeService;
	private PersonService personService;
	private AddressService addressService;
	private HttpServletRequest request;
	private List<Employee> employeesList = new ArrayList<Employee>();
	private List<Address> addressesList = new ArrayList<Address>();
	private List<EmployeeBO> employeeBOsList = new ArrayList<EmployeeBO>();
	private List<CodeValueData> employeeHeaderList = new ArrayList<CodeValueData>();
	
	private List<CodeValueData> salutationList = new ArrayList<CodeValueData>();
	private List<CodeValueData> shiftList = new ArrayList<CodeValueData>();
	private List<CodeValueData> genderList = new ArrayList<CodeValueData>();
	private List<CodeValueData> statusList = new ArrayList<CodeValueData>();


	private final String SALUTATION = "common.person.salutation";
	private final String SHIFT = "common.employee.shift";
	private final String GENDER = "common.person.gender";
	private final String EMPLOYEE_HEADER = "common.employee.header";
	private final String STATUS = "common.user.status";

	public String execute(){
		loadAction();
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	private void loadAction() {
		salutationList = propertyManager.getInstance().getListFromProp(SALUTATION);
		employeeHeaderList = propertyManager.getInstance().getListFromProp(EMPLOYEE_HEADER);
		genderList = propertyManager.getInstance().getListFromProp(GENDER);
		shiftList = propertyManager.getInstance().getListFromProp(SHIFT);
		statusList = propertyManager.getInstance().getListFromProp(STATUS);
		
		String Id = getServletRequest().getParameter("id");
		setCrudType(getServletRequest().getParameter("pType"));
		if(Id!=null&&!Id.isEmpty()){
			employee = (Employee) employeeService.findById(Employee.class, Integer.parseInt(Id) );
		}
		if(crudType!=null && employee!=null && crudType.equalsIgnoreCase("Delete")){
			addressesList = addressService.getAddressesOfPerson(employee.getPerson().getPersonId());
			for (Address address : addressesList) {
				addressService.txDelete(address);
			}
			employeeService.txDelete(employee);
			addActionMessage("Record deleted successfully");
			
		}
		if(crudType!=null && crudType.equalsIgnoreCase("Edit")){
			addressesList = addressService.getAddressesOfPerson(employee.getPerson().getPersonId());
			if(addressesList!=null && addressesList.size()>0){
				address = addressesList.get(0);
				return;
			}
		}
		
			employeesList = employeeService.findAll(Employee.class);
			if(employeesList.size()>0){
				for (Employee emp : employeesList) {
					if(emp!=null && emp.getPerson()!=null){
						if(emp.getPerson().getPersonId()>0){
							addressesList = addressService.getAddressesOfPerson(emp.getPerson().getPersonId());
						}
					String EmployeeId = "";
					Address address1 = new Address();
						if(addressesList!=null && addressesList.size()>0){
							address1 = addressesList.get(0);
						}
						if(emp.getEmployee()!=null){
							EmployeeId = emp.getEmployee().getEmployeeCode();
						}
					employeeBOsList.add(new EmployeeBO(emp, address1, emp.getPerson().getGender(), emp.getStatus(),emp.getShift(), EmployeeId));
					}
				}
			}
		

		
	}

	@SuppressWarnings("unchecked")
	public String addEmployee() throws Exception{

		address.setPerson(employee.getPerson());
		addressService.txSaveOrUpdateObject(address);
		employeeService.txSaveOrUpdate(employee);
		
		return SUCCESS;
 
	}
	
	public List<CodeValueData> getSalutationList() {
		return salutationList;
	}

	public void setSalutationList(List<CodeValueData> salutationList) {
		this.salutationList = salutationList;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public List<CodeValueData> getShiftList() {
		return shiftList;
	}

	public void setShiftList(List<CodeValueData> shiftList) {
		this.shiftList = shiftList;
	}

	/**
	 * @param genderList the genderList to set
	 */
	public void setGenderList(List<CodeValueData> genderList) {
		this.genderList = genderList;
	}

	/**
	 * @return the genderList
	 */
	public List<CodeValueData> getGenderList() {
		return genderList;
	}

	/**
	 * @param statusList the statusList to set
	 */
	public void setStatusList(List<CodeValueData> statusList) {
		this.statusList = statusList;
	}

	/**
	 * @return the statusList
	 */
	public List<CodeValueData> getStatusList() {
		return statusList;
	}

	/**
	 * @return the employeeService
	 */
	public EmployeeService getEmployeeService() {
		return employeeService;
	}

	/**
	 * @param employeeService the employeeService to set
	 */
	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}
	
	public HttpServletRequest getServletRequest(){
		return request;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		// TODO Auto-generated method stub
		this.request = request;
	}

	/**
	 * @return the request
	 */
	public HttpServletRequest getRequest() {
		return request;
	}
	

	/**
	 * @param request the request to set
	 */
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	/**
	 * @return the crudType
	 */
	public String getCrudType() {
		return crudType;
	}

	/**
	 * @param crudType the crudType to set
	 */
	public void setCrudType(String crudType) {
		this.crudType = crudType;
	}

	/**
	 * @return the employeesList
	 */
	public List<Employee> getEmployeesList() {
		return employeesList;
	}

	/**
	 * @param employeesList the employeesList to set
	 */
	public void setEmployeesList(List<Employee> employeesList) {
		this.employeesList = employeesList;
	}


	/**
	 * @return the employeeBOsList
	 */
	public List<EmployeeBO> getEmployeeBOsList() {
		return employeeBOsList;
	}

	/**
	 * @param employeeBOsList the employeeBOsList to set
	 */
	public void setEmployeeBOsList(List<EmployeeBO> employeeBOsList) {
		this.employeeBOsList = employeeBOsList;
	}

	/**
	 * @return the employeeHeaderList
	 */
	public List<CodeValueData> getEmployeeHeaderList() {
		return employeeHeaderList;
	}

	/**
	 * @param employeeHeaderList the employeeHeaderList to set
	 */
	public void setEmployeeHeaderList(List<CodeValueData> employeeHeaderList) {
		this.employeeHeaderList = employeeHeaderList;
	}

	/**
	 * @return the personService
	 */
	public PersonService getPersonService() {
		return personService;
	}

	/**
	 * @param personService the personService to set
	 */
	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	/**
	 * @return the addressService
	 */
	public AddressService getAddressService() {
		return addressService;
	}

	/**
	 * @param addressService the addressService to set
	 */
	public void setAddressService(AddressService addressService) {
		this.addressService = addressService;
	}

	/**
	 * @return the address
	 */
	public Address getAddress() {
		return address;
	}

	/**
	 * @param address the address to set
	 */
	public void setAddress(Address address) {
		this.address = address;
	}

	/**
	 * @return the addressesList
	 */
	public List<Address> getAddressesList() {
		return addressesList;
	}

	/**
	 * @param addressesList the addressesList to set
	 */
	public void setAddressesList(List<Address> addressesList) {
		this.addressesList = addressesList;
	}

	/**
	 * @return the managerIdStr
	 */
	public String getManagerIdStr() {
		return managerIdStr;
	}

	/**
	 * @param managerIdStr the managerIdStr to set
	 */
	public void setManagerIdStr(String managerIdStr) {
		this.managerIdStr = managerIdStr;
	}

}
